package com.tsingteng.cosfun.ui.login;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsingteng.cosfun.MainActivity;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterActivity;
import com.tsingteng.cosfun.bean.ReportBean;
import com.tsingteng.cosfun.bean.UserBean;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.ui.message.CommonActivity;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.KeyBoardUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.widget.TitleView;
import com.umeng.qq.tencent.Tencent;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;


import butterknife.BindView;
import butterknife.OnClick;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/8
 * qq(872179966)
 */

public class LoginActivity extends BasePresenterActivity<LogonPresenter, LogonContract.ILogonView> implements LogonContract.ILogonView, TextWatcher {
    @BindView(R.id.tv_title_view)
    TitleView mTvTitle;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_verify_code)
    EditText etVerifyCode;
    @BindView(R.id.tv_bt_verify_code)
    TextView tvBtVerifyCode;
    @BindView(R.id.ll_bt_login)
    LinearLayout llBtLogin;
    private String mMobile;
    private String mVerifyCode;
    private int LOGIN_PHONE = 0;
    public int LOGIN_QQ = 1;
    private int LOGIN_WX = 2;
    private int LOGIN_WB = 3;
    private boolean isVerify = false;
    private boolean isMain;
    private int type;


    @Override
    public String getUserName() {
        mMobile = etMobile.getText().toString().trim();
        return mMobile;
    }

    @Override
    public String getVerifyCode() {
        isVerify = false;
        mVerifyCode = etVerifyCode.getText().toString().trim();
        return mVerifyCode;
    }

    @Override
    public void showLogonResult(UserBean data) {
        showHintDialog("登录成功!");
//        HomeMineFragment.isNeedFresh = true;
        //TODO 登录成功回调
        llBtLogin.setEnabled(true);
        if (isMain) {
            Intent intent = new Intent( this, MainActivity.class);
            intent.putExtra("type",type);
            startActivity(intent);
        }
        finish();
//        logonUpload();
        overridePendingTransition(R.anim.activity_up_alpha, R.anim.activity_down);


    }



    @Override
    public void showVerifyCodeResult(UserBean data) {
        if (data.getStatus() == 1) {
            LogonUtils.runTime(tvBtVerifyCode);
            tvBtVerifyCode.setBackgroundColor(getResources().getColor(R.color.white_alpha_55));
            showToast("验证码发送成功!");
            isVerify = true;
        }


    }

    @Override
    public void showQQLogonResult(UserBean userBean) {
        Navigate.startMain(this,false);
        finish();
//        logonUpload();
    }

    /**
     * 友盟登录上报
     *
     * @param status
     */
    @Override
    public void showReportData(ReportBean status) {
//        showToast(status.toString());
        Log.e("登录后上报", status.toString());
    }


    @Override
    protected void initData() {
        isFound = true;
        mTvTitle.setIvLeft(R.drawable.close_icon);
        mTvTitle.setTitle("手机号登录");
        KeyBoardUtils.losePoint(etMobile);
        KeyBoardUtils.losePoint(etVerifyCode);
        etMobile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyBoardUtils.searchPoint(etMobile);
            }
        });
        etVerifyCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyBoardUtils.searchPoint(etVerifyCode);
            }
        });
        mTvTitle.setOnTitleClickListener(new TitleView.OnTitleClickListener() {
            @Override
            public void onLeft() {

                finish();
                overridePendingTransition(R.anim.activity_up_alpha, R.anim.activity_down);
            }

            @Override
            public void onRight() {

            }

            @Override
            public void onRight2() {

            }
        });

        KeyBoardUtils.observeSoftKeyboard(this, new KeyBoardUtils.OnSoftKeyboardChangeListener() {
            @Override
            public void onSoftKeyBoardChange(int softKeybardHeight, boolean visible) {
                if (visible) {

                } else {
                    KeyBoardUtils.losePoint(etMobile);
                    KeyBoardUtils.losePoint(etVerifyCode);
                }
            }
        });
        etMobile.addTextChangedListener(this);
        etVerifyCode.addTextChangedListener(this);
    }

    @Override
    protected LogonPresenter createPresenter() {
        return new LogonPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login;
    }

    @Override
    protected void getIntentData(Intent intent) {
        isMain = intent.getBooleanExtra("isMain", false);
        type = intent.getIntExtra("type", -1);


    }

    @OnClick({R.id.tv_bt_verify_code, R.id.ll_bt_login, R.id.ll_qq, R.id.ll_wx, R.id.ll_wb, R.id.tv_agreement,R.id.tv_intimate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bt_verify_code:
                mPresenter.verifyCode();
                break;
            case R.id.ll_bt_login:
                showLoading("正在登录...");
                logon(LOGIN_PHONE);
                break;
            case R.id.ll_qq:
                showLoading("正在登录...");
                logon(LOGIN_QQ);
                break;
            case R.id.ll_wx:
                showLoading("正在登录...");
                logon(LOGIN_WX);
                break;
            case R.id.ll_wb:
                showLoading("正在登录...");
                logon(LOGIN_WB);
                break;
            case R.id.tv_agreement:
                Navigate.startCommont(this, CommonActivity.KEY_AGREEMENT,1);
                break;
            case R.id.tv_intimate:
                Navigate.startCommont(this, CommonActivity.KEY_AGREEMENT,2);
                break;
        }
    }

    public void logon(int type) {
        switch (type) {
            case 0:
                AppUtils.setLoginTag(Config.LOGIN_MOBILE);
                mPresenter.login();
                break;
            case 1:
                logonQQ();
                break;
            case 2:
                logonWX();
                break;
            case 3:
                logonWB();
                break;
        }
    }


    /**
     * 微博登录
     */
    private void logonWB() {
        LogonUtils.umengDeleteOauth(this, SHARE_MEDIA.SINA);
        LogonUtils.shareLoginUmeng(this, SHARE_MEDIA.SINA, getOnLogonListener());
    }

    @NonNull
    private LogonUtils.OnLogonListener getOnLogonListener() {
        return new LogonUtils.OnLogonListener() {
            @Override
            public void onStartLogon() {
                showLoading("正在启动...");
            }

            @Override
            public void onCompleteLogon(SHARE_MEDIA share_media_type, String openid, String access_token, String iconurl, String name) {
                hideLoading();
                if (share_media_type == SHARE_MEDIA.QQ) {
                    mPresenter.logonQQ(openid, access_token, "3", iconurl, name);
                } else if (share_media_type == SHARE_MEDIA.WEIXIN) {
                    mPresenter.logonQQ(openid, access_token, "5", iconurl, name);
                } else {
                    mPresenter.logonQQ(name, access_token, "4", iconurl, name);
                }

            }

            @Override
            public void onCancleLogon() {
                hideLoading();
            }

            @Override
            public void onErrorLogon(Throwable throwable) {
                hideLoading();
            }
        };
    }


    /**
     * 微信登录
     */
    private void logonWX() {
        LogonUtils.umengDeleteOauth(this, SHARE_MEDIA.WEIXIN);
        LogonUtils.shareLoginUmeng(this, SHARE_MEDIA.WEIXIN, getOnLogonListener());
    }

    /**
     * QQ登录
     */
    private void logonQQ() {
        LogonUtils.umengDeleteOauth(this, SHARE_MEDIA.QQ);
        LogonUtils.shareLoginUmeng(this, SHARE_MEDIA.QQ, getOnLogonListener());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        Log.e("umengDeleteOauth", "onActivityResult: " + requestCode + "\n" + resultCode + "\n" + data);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (etVerifyCode.length() == 4 && etMobile.length() == 11) {
            llBtLogin.setBackground(getResources().getDrawable(R.color.color_found));
        } else {
            llBtLogin.setBackground(getResources().getDrawable(R.color.bt_bg_login));
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.activity_up_alpha, R.anim.activity_down);
    }
}
